import _ from 'lodash'
import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router"
import {setupStudioRouter} from 'one-code-vue'

export const Layout = () => import("@/layout/index.vue");

/**
 * 利用vite扫描所有的页面组件 | /src/pages/Demo.vue -> import('/src/pages/Demo.vue') -> Module.default
 */
const pages = import.meta.glob(
    ['@/pages/**/*.vue'],
    {eager: false},
);
window['pages'] = pages

/**
 * 从 widgets 文件夹中分析出所有的 widget
 */
const widgetModule = import.meta.globEager('@/widgets/**/*.js')
const widgets = {}
_.forOwn(widgetModule, (value: any, key: string) => {
    // key '/src/widgets/AreaSearch.js' => AreaSearch
    if (_.startsWith(key, '/src/widgets/')) {
        const name = key.substring(13, key.length - 3)
        widgets[name] = value.default
        widgets[name]._filePath = key
    }
})
window['widgets'] = widgets

/**
 * 允许启用 OneCodeStudio
 * 从所有页面分析出所有 designJS文件
 */
const studioEnable = import.meta.env.VITE_APP_STUDIO_ENABLE
if (studioEnable) {
    const designJS = {};

    const designJsModule = import.meta.glob('@/pages/**/*.design.js', {eager: true})
    _.forOwn(designJsModule, (value: any, key: string) => {
        if (key.startsWith("/src/pages")) {
            key = key.substring(11);
        }
        designJS[key] = async () => {
            return await value();
        };
    })
    window['designJS'] = designJS

    /**
     * 从 ui-mobile 文件夹中分析出所有的移动端页面
     */
    const mobilePages = import.meta.env.VITE_MOBILE_PAGES
    if (mobilePages) {
        const mobilePages = import.meta.globEager('../../ui-mobile/src/pages/**/*.vue')
        window['pages_mobile'] = mobilePages
    }
}

/**
 * 静态路由表定义
 */
export const constantRoutes: RouteRecordRaw[] = [
    {
        path: "/redirect",
        component: Layout,
        meta: {hidden: true},
        children: [
            {
                path: "/redirect/:path(.*)",
                component: () => import("@/views/redirect.vue"),
            },
        ],
    },

    {
        path: "/login",
        component: () => import("@/pages/login.vue"),
        meta: {hidden: true},
    },

    {
        path: "/",
        component: Layout,
        redirect: "/dashboard",
        children: [
            {
                path: "dashboard",
                component: () => import("@/pages/dashboard.vue"),
                name: "Dashboard",
                meta: {title: "dashboard", icon: "homepage", affix: true},
            },
            {
                path: "401",
                component: () => import("@/views/error-page/401.vue"),
                meta: {hidden: true},
            },
            {
                path: "404",
                component: () => import("@/views/error-page/404.vue"),
                meta: {hidden: true},
            },
        ],
    },
];

/**
 * VITE_APP_STUDIO_ENABLE 定义是否启用 studio
 */
if (import.meta.env.VITE_APP_STUDIO_ENABLE === 'true') {
    // 安装 studio 的路由
    setupStudioRouter(constantRoutes, '/one-code-studio/')
}

/**
 * 创建路由器
 */
const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes as RouteRecordRaw[],
    // 刷新时，滚动条位置还原
    scrollBehavior: () => ({left: 0, top: 0}),
});

/**
 * 重置路由
 */
export function resetRouter() {
    router.replace({path: "/login"});
}

/**
 * 获取路由数据
 */
async function getRoutes(): Promise<RouteRecordRaw[]> {
    return [];
}

export default router;
